home *** CD-ROM | disk | FTP | other *** search
/ PC User 2008 February / Australian_PC_User_2008-02.iso / magstuff / files / Visual Basic / NumberToText_VBCode.txt < prev   
Encoding:
Text File  |  2007-12-03  |  4.6 KB  |  130 lines

  1. Public Class Form1
  2.  
  3.     'Main Function
  4.     Function SpellNumber(ByVal MyNumber)
  5.         Dim Dollars, Cents, Temp
  6.         Dim DecimalPlace, Count
  7.         Dim Place(9) As String
  8.         Place(2) = " Thousand "
  9.         Place(3) = " Million "
  10.         Place(4) = " Billion "
  11.         Place(5) = " Trillion "
  12.         ' String representation of amount.
  13.         MyNumber = Trim(Str(MyNumber))
  14.         ' Position of decimal place 0 if none.
  15.         DecimalPlace = InStr(MyNumber, ".")
  16.         ' Convert cents and set MyNumber to dollar amount.
  17.         If DecimalPlace > 0 Then
  18.             Cents = GetTens(Strings.Left(Strings.Mid(MyNumber, DecimalPlace + 1) & "00", 2))
  19.             MyNumber = Trim(Strings.Left(MyNumber, DecimalPlace - 1))
  20.         End If
  21.         Count = 1
  22.         Do While MyNumber <> ""
  23.             Temp = GetHundreds(Strings.Right(MyNumber, 3))
  24.             If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
  25.             If Len(MyNumber) > 3 Then
  26.                 MyNumber = Strings.Left(MyNumber, Strings.Len(MyNumber) - 3)
  27.             Else
  28.                 MyNumber = ""
  29.             End If
  30.             Count = Count + 1
  31.         Loop
  32.         Select Case Dollars
  33.             Case ""
  34.                 Dollars = "No Dollars"
  35.             Case "One"
  36.                 Dollars = "One Dollar"
  37.             Case Else
  38.                 Dollars = Dollars & " Dollars"
  39.         End Select
  40.         Select Case Cents
  41.             Case ""
  42.                 Cents = " and No Cents"
  43.             Case "One"
  44.                 Cents = " and One Cent"
  45.             Case Else
  46.                 Cents = " and " & Cents & " Cents"
  47.         End Select
  48.         SpellNumber = Dollars & Cents
  49.     End Function
  50.  
  51.     ' Converts a number from 100-999 into text 
  52.     Function GetHundreds(ByVal MyNumber)
  53.         Dim Result As String
  54.         If Val(MyNumber) = 0 Then Exit Function
  55.         MyNumber = Strings.Right("000" & MyNumber, 3)
  56.         ' Convert the hundreds place.
  57.         If Mid(MyNumber, 1, 1) <> "0" Then
  58.             Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
  59.         End If
  60.         ' Convert the tens and ones place.
  61.         If Mid(MyNumber, 2, 1) <> "0" Then
  62.             Result = Result & GetTens(Mid(MyNumber, 2))
  63.         Else
  64.             Result = Result & GetDigit(Mid(MyNumber, 3))
  65.         End If
  66.         GetHundreds = Result
  67.     End Function
  68.  
  69.     ' Converts a number from 10 to 99 into text. 
  70.     Function GetTens(ByVal TensText)
  71.         Dim Result As String
  72.         Result = ""           ' Null out the temporary function value.
  73.         If Val(Strings.Left(TensText, 1)) = 1 Then   ' If value between 10-19...
  74.             Select Case Val(TensText)
  75.                 Case 10 : Result = "Ten"
  76.                 Case 11 : Result = "Eleven"
  77.                 Case 12 : Result = "Twelve"
  78.                 Case 13 : Result = "Thirteen"
  79.                 Case 14 : Result = "Fourteen"
  80.                 Case 15 : Result = "Fifteen"
  81.                 Case 16 : Result = "Sixteen"
  82.                 Case 17 : Result = "Seventeen"
  83.                 Case 18 : Result = "Eighteen"
  84.                 Case 19 : Result = "Nineteen"
  85.                 Case Else
  86.             End Select
  87.         Else                                 ' If value between 20-99...
  88.             Select Case Val(Strings.Left(TensText, 1))
  89.                 Case 2 : Result = "Twenty "
  90.                 Case 3 : Result = "Thirty "
  91.                 Case 4 : Result = "Forty "
  92.                 Case 5 : Result = "Fifty "
  93.                 Case 6 : Result = "Sixty "
  94.                 Case 7 : Result = "Seventy "
  95.                 Case 8 : Result = "Eighty "
  96.                 Case 9 : Result = "Ninety "
  97.                 Case Else
  98.             End Select
  99.             Result = Result & GetDigit(Strings.Right(TensText, 1))
  100.         End If
  101.         GetTens = Result
  102.     End Function
  103.  
  104.     ' Converts a number from 1 to 9 into text. 
  105.     Function GetDigit(ByVal Digit)
  106.         Select Case Val(Digit)
  107.             Case 1 : GetDigit = "One"
  108.             Case 2 : GetDigit = "Two"
  109.             Case 3 : GetDigit = "Three"
  110.             Case 4 : GetDigit = "Four"
  111.             Case 5 : GetDigit = "Five"
  112.             Case 6 : GetDigit = "Six"
  113.             Case 7 : GetDigit = "Seven"
  114.             Case 8 : GetDigit = "Eight"
  115.             Case 9 : GetDigit = "Nine"
  116.             Case Else : GetDigit = ""
  117.         End Select
  118.     End Function
  119.  
  120.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  121.         Dim numbToConvert As String
  122.         numbToConvert = Val(TextBox1.Text)
  123.         MsgBox(SpellNumber(numbToConvert))
  124.     End Sub
  125.  
  126.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  127.         End
  128.     End Sub
  129. End Class
  130.